home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_300 / 353_02 / date.cpp < prev    next >
C/C++ Source or Header  |  1992-01-18  |  4KB  |  136 lines

  1.                                // Chapter 5 - Program 11
  2.  
  3. // This file contains the implementation for the date class.
  4.  
  5. #include <stdio.h>       // Prototype for sprintf
  6. #include <time.h>        // Prototypes for the current date
  7. #include "date.h"
  8.  
  9. char date::format;         // This defines the static data member
  10. char date::out_string[25]; // This defines the static string
  11.  
  12.          // Constructor - Set date to current date, and
  13.          //               set format to the default of 1
  14. date::date(void)
  15. {
  16. time_t time_date;
  17. struct tm *current_date;
  18.  
  19.    time_date = time(NULL);                // DOS system call
  20.    current_date = localtime(&time_date);  // DOS system call
  21.    month = current_date->tm_mon + 1;
  22.    day = current_date->tm_mday;
  23.    year = current_date->tm_year + 1900;
  24.    format = 1;
  25. }
  26.  
  27.  
  28.          // Set the date to these input parameters
  29.          //  if return = 0 ---> All data is valid
  30.          //  if return = 1 ---> Something out of range
  31. int date::set_date(int in_month, int in_day, int in_year)
  32. {
  33. int temp = 0;
  34. int max_days;
  35.                       // The limits on the year are purely arbitrary
  36.    if (in_year < 1500) {           // Check that the year is between
  37.       year = 1500;                 //  1500 and 2200
  38.       temp = 1;
  39.    } else {
  40.       if (in_year > 2200) {
  41.          year = 2200;
  42.          temp = 1;
  43.       } else
  44.          year = in_year;
  45.    }
  46.  
  47.    if(in_month < 1) {              // Check that the month is between
  48.       month = temp = 1;            //  1 and 12
  49.    } else {
  50.       if (in_month > 12) {
  51.          month = 12;
  52.          temp = 1;
  53.       } else
  54.          month = in_month;
  55.    }
  56.  
  57.    max_days = days_this_month();
  58.    if (in_day < 1) {               // Check that the day is between
  59.       day = temp = 1;              //  1 and max_days
  60.    } else {
  61.       if (in_day > max_days) {
  62.          day = max_days;
  63.          temp = 1;
  64.       } else
  65.          day = in_day;
  66.    }
  67.  
  68.    return temp;
  69. }
  70.  
  71.  
  72.  
  73. static char *month_string[13] = {" ", "Jan", "Feb", "Mar", "Apr",
  74.                                       "May", "Jun", "Jul", "Aug",
  75.                                       "Sep", "Oct", "Nov", "Dec"};
  76.  
  77.          // Return Jan Feb Mar Apr etc.
  78. char *date::get_month_string(void)
  79. {
  80.    return month_string[month];
  81. }
  82.  
  83.  
  84.  
  85.          // Return an ASCII-Z string depending on the stored format
  86.          //   format = 1    Aug 29, 1991
  87.          //   format = 2    8/29/91
  88.          //   format = 3    8/29/1991
  89.          //   format = 4    29 Aug 1991    Military time
  90.          //   format = ?    Anything else defaults to format 1
  91. char *date::get_date_string(void)
  92. {
  93.    switch (format) {
  94.                        // This printout assumes that the year will be
  95.                        //  between 1900 and 1999
  96.       case 2  : sprintf(out_string, "%02d/%02d/%02d",
  97.                                             month, day, year - 1900);
  98.                 break;
  99.  
  100.       case 3  : sprintf(out_string, "%02d/%02d/%04d",
  101.                                                    month, day, year);
  102.                 break;
  103.  
  104.       case 4  : sprintf(out_string, "%d %s %04d",
  105.                                      day, month_string[month], year);
  106.                 break;
  107.  
  108.       case 1  : // Fall through to the default case
  109.       default : sprintf(out_string, "%s %d, %04d",
  110.                                      month_string[month], day, year);
  111.                 break;
  112.    }
  113.    return out_string;
  114. }
  115.  
  116.  
  117.  
  118. int days[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  119.  
  120.          // Since this is declared in the private part of the class
  121.          //  header is is only available for use within the class.
  122.          //  It is hidden from use outside of the class.
  123. int date::days_this_month(void)
  124. {
  125.    if (month != 2)
  126.       return days[month];
  127.  
  128.    if (year % 4)       // Not leap year
  129.       return 28;
  130.    if (year % 100)     // It is leap year
  131.       return 29;
  132.    if (year % 400)     // Not leap year
  133.       return 28;
  134.    return 29;          // It is leap year
  135. }
  136.